import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import fire from '@dfeidao/atom-web/msg/fire';
import error from '@dfeidao/atom-web/msgbox/error';

import nodejs from '@dfeidao/atom-web/msg/nodejs';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';


interface Aa {
	id: string;
	p_id: string;
	flag: number;
	client_point_id: string;
	client_style: string;
}


export default async function a002(fd: IFeidaoAiBrowserComponent) {
	const curr_dept = get(fd, 'curr_dept') as Aa;
	const client_point_id: string = get(fd, 'client_point_id');
	const client_style: string = get(fd, 'client_style');
	const flag = get(fd, 'flag');

	if (flag === 0) {
		error('请点击新增同级');
		return;
	}

	if (!curr_dept) {
		error('请先选中一条数据');

		return;
	}

	curr_dept.flag = 2;// 新增下级
	curr_dept.client_point_id = client_point_id;
	curr_dept.client_style = client_style;
	const menu_no = curr_dept.id;
	const productid = get(fd, 'productid');
	const client_type = curr_dept.client_point_id;

	set(fd, 'curr_dept', curr_dept);
	if (client_type === '2' || client_type === '3') {
		const res = await nodejs('menu/zj-000001/s001', { menu_no, productid, client_type });
		// debugger;
		if (res === '3' && client_type === '3') {
			error('此类型客户端最多只能有三级菜单');
		} else if (res >= 2 && client_type === '2') {
			error('此类型客户端最多只能有二级菜单');
		} else {
			fire('zj-000002', 'ec001', curr_dept);
		}
	} else {
		fire('zj-000002', 'ec001', curr_dept);
	}
}
